<div class="panel panel-default">
<div class="panel-heading">上传excel</div>
<div class="panel-body">
<form id="form1" name="form1" method="post" class="form-horizontal" action="">
  <div class="form-group">
      <label class="col-sm-2 control-label">文件</label>
      <div class="col-sm-8">
          <input type="file" class="js_upload form-control" accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" placeholder="" >
          <span id="progress"></span>
      </div>
  </div>

  <div class="form-group">
      <label class="col-sm-2 control-label"></label>
      <div class="col-sm-8">
          注意：请上传格式后缀为xlsx的文件,工作表不要包含特殊格式；
          <br/><a href="__CDN__/uploads/salary/temp2.xlsx">模板下载</a>
      </div>
  </div>
</form>
</div>
</div>


<div class="panel panel-default">
<div class="panel-heading">导入</div>
<div class="panel-body">
  <form id="form2" name="form2" method="post" class="form-horizontal" action="">
  <div class="form-group">
      <label class="col-sm-2 control-label">年份</label>
      <div class="col-sm-8">
          <select name="year" id="year" class="form-control">
              <option value="{php}echo date('Y'){/php}">{php}echo date('Y'){/php}</option>
              <option value="{php}echo date('Y', strtotime('+1 year')){/php}">{php}echo date('Y', strtotime('+1 year')){/php}</option>
              <option value="{php}echo date('Y', strtotime('-1 year')){/php}">{php}echo date('Y', strtotime('-1 year')){/php}</option>
          </select>
      </div>
  </div>

  <div class="form-group">
      <label class="col-sm-2 control-label">月份</label>
      <div class="col-sm-8">
          <select name="month" id="month" class="form-control">

          {php}
            for ($i = 1; $i < 13; $i++) {
                echo '<option value="'.$i.'">'.$i.'</option>';
            }
          {/php}

          </select>

      </div>
  </div>

  <div class="form-group">
      <label class="col-sm-2 control-label">文件</label>
      <div class="col-sm-8">
          <select name="filename" id="filename" class="form-control">
          </select>

      </div>
  </div>

  <div class="form-group">
      <label class="col-sm-2 control-label"></label>
      <div class="col-sm-8">
          <a class="btn btn-info" id="start">开始导入数据</a>
      </div>
  </div>
</form>
</div>
</div>
<script type="text/javascript" src="__CDN__/assets/libs/jquery/dist/jquery.min.js"></script>
<script type="text/javascript">
function getxlsxlist() {
    $.post("{:url('salary/getxlsxlist')}", function(res) {
        $('#filename').html(res);
    }, 'html');
}
$(function() {
    getxlsxlist();

    $(document).on('click', '#start' ,function(){
         var year=$("#year").val();
         var month=$("#month").val();
         var filename=$("#filename").val();

         $("#start").html("正在导入");
         $.ajax({
                  type : 'post',
                  url : "/exceldoneafew3rfs.php",
                  data : {year:year,month:month,filename:filename},
                  dataType : 'JSON',
                  success: function(jsonData ) {
                      if (jsonData.status == 1) {
                            layer.msg('导入成功！');

                            $("#start").html(jsonData.info);

                            setTimeout(function() {
                                // 关闭当前的 iframe
                                window.parent.layer.close(window.parent.layer.getFrameIndex(window.name));

                                // 刷新父级页面的数据
                                window.parent.location.reload();
                            }, '2000')
                      }
                      else {
                        alert(jsonData.info);
                        $("#start").html("开始导入数据");
                        return false;
                      }
                  }
              });

    });
});
</script>

<script type="text/javascript">
$(function() {
    // 上传文件
    $(".js_upload").change(function() {
        // new FormData object
        var formData = new FormData();
        formData.append('file', $(this)[0].files[0]);
        var the = $('#progress');

        // 上传
        $.ajax({
            url: "{:url('ajax/upload2')}",
            type: 'POST',
            cache: false,
            data: formData,
            dataType: "json",
            processData: false,
            contentType: false,
            xhr: function(){
                var xhr = $.ajaxSettings.xhr();
                console.log(xhr)
                if(onprogress && xhr.upload) {

                    xhr.upload.addEventListener("progress" , onprogress, false);
                    return xhr;
                }
            }
        }).done(function(res) {
            // check
            if(res.code == 1) {
                getxlsxlist();

                the.html('上传成功');


            } else if (res.code == 0) {
                alert(res.msg);
            }
        }).fail(function(res) {
            console.log(res);
        });

        /**
         * 侦查附件上传情况, 这个方法大概0.05-0.1秒执行一次
         *
         */
        function onprogress(evt){
            console.log(evt);
            var loaded = evt.loaded;                  //已经上传大小情况
            var tot = evt.total;                      //附件总大小
            var per = Math.floor(100*loaded/tot);     //已经上传的百分比
            $('#progress').html('上传中...' + per + "%");
            // $("#son").html( per +"%" );
            // $("#son").css("width" , per +"%");
        }

        // 清空 input value 避免上传同一张照片时第二次失效
    });

});
</script>